package com.clinic.logic.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.clinic.accessor.db.AssistantAccessor;
import com.clinic.accessor.db.UserContactAccessor;
import com.clinic.entity.db.Assistant;
import com.clinic.entity.db.UserContact;
import com.clinic.entity.gui.GuiAssistant;
import com.clinic.entity.gui.GuiPagination;
import com.clinic.entity.gui.GuiUserContact;
import com.clinic.entity.param.Index;
import com.clinic.util.CommonUtil;
import com.clinic.util.ConvertUtil;

@Component
public class AssistantLogic {
	
	@Autowired
	private AssistantAccessor assistantAccessor;
	
	@Autowired
	private UserContactAccessor userContactAccessor;
	
	public GuiAssistant insertAssistant(GuiAssistant guiAssistant) {
		
		Assistant assistant = ConvertUtil.getAssistantByGuiAssistant(guiAssistant, new Assistant());
		Assistant iAssistant = assistantAccessor.insertOrUpdate(assistant);
		
		GuiUserContact guiContact = guiAssistant.getContact();
		UserContact contact = ConvertUtil.getUserContactByGuiUserContact(guiContact, new UserContact());
		contact.setUser(iAssistant);
		UserContact iContact = userContactAccessor.insertOrUpdate(contact);
		
		GuiAssistant uAssistant = ConvertUtil.getGuiAssistantByAssistant(iAssistant);
		GuiUserContact uContact = ConvertUtil.getGuiUserContactByUserContact(iContact);
		uContact.setUser(uAssistant);
		
		uAssistant.setContact(uContact);
		
		return uAssistant;
	}
	
	public GuiAssistant updateAssistant(GuiAssistant guiAssistant) {
		
		Assistant assistant = assistantAccessor.getAssistantById(guiAssistant.getUserId());
		Assistant oAssistant = ConvertUtil.getAssistantByGuiAssistant(guiAssistant, assistant);
		UserContact contact = assistant.getContact();
		if(contact == null) {
			contact = new UserContact();
		}
		UserContact oContact = ConvertUtil.getUserContactByGuiUserContact(guiAssistant.getContact(), contact);
		oContact.setUser(oAssistant);
		oAssistant.setContact(oContact);
		Assistant uAssistant = assistantAccessor.insertOrUpdate(oAssistant);
		
		return ConvertUtil.getGuiAssistantByAssistant(uAssistant);
	}
	
	public GuiAssistant getAssistantDetailsById(long id) {
		
		Assistant assistant = assistantAccessor.getAssistantById(id);
		GuiAssistant result = ConvertUtil.getGuiAssistantByAssistant(assistant);
		result.setContact(ConvertUtil.getGuiUserContactByUserContact(assistant.getContact()));
		return result;
	}
	
	public boolean isValidAssistantUpdate(GuiAssistant guiAssistant) {

		Assistant result = assistantAccessor.getAssistantById(guiAssistant.getUserId());
		return (result != null);
	}	
	
	public boolean isValidInsert(GuiAssistant guiAssistant) {
		return !isNotValidInsert(guiAssistant);
	}
	
	public boolean isNotValidInsert(GuiAssistant guiAssistant) {

		return StringUtils.isBlank(guiAssistant.getName()) ||
				StringUtils.isBlank(guiAssistant.getType()) ||
				StringUtils.isBlank(guiAssistant.getContact().getAddress()) ||
				StringUtils.isBlank(guiAssistant.getContact().getTelNo()) ||
				StringUtils.isBlank(guiAssistant.getQualifications());
	}
	
	public List<GuiAssistant> getAssistantList(Index index) {
		List<Assistant> assistantList = assistantAccessor.getAllAssistants(index);
		return getGuiAssistantsByAssistants(assistantList);
	}
	
	public List<GuiAssistant> getAssistantListByTerm(String term, String column, Index index) {
		List<Assistant> assistantList = assistantAccessor.getOrderedAssistantsByTerm(term, column, index);		
		return getGuiAssistantsByAssistants(assistantList);
	}
	
	public void deleteAssistantDetailsById(long id) {
		Assistant assistant = assistantAccessor.getAssistantById(id);
		if (assistant != null) {
			// TODO delete other related data before ... appointments, contacts... etc
			assistantAccessor.delete(assistant);
		}
	}
	
	public GuiPagination getPaginationOfAssistantsByTerm(String term, String column, int page) {
		int total = assistantAccessor.countOfOrderedAssistantsByTerm(term, column);
		return CommonUtil.getPaginationInfo(page, total);
	}
	
	public GuiPagination getPaginationOfAssistants(int page) {
		int total = assistantAccessor.countAssistants();
		return CommonUtil.getPaginationInfo(page, total);
	}
	
	private List<GuiAssistant> getGuiAssistantsByAssistants(List<Assistant> assistantList) {
		List<GuiAssistant> result = new ArrayList<>();
		for (Assistant assistant : assistantList) {
			result.add(ConvertUtil.getGuiAssistantByAssistant(assistant));
		}
		return result;
	}
	
}
